package com.codrim.cs.data.server.dao.mapper;

import com.codrim.cs.data.rpc.dto.param.AdvertiserWholeDayDataForPeriodParamDTO;
import com.codrim.cs.data.server.dao.entity.StatCsAdvAllDO;
import com.codrim.cs.data.server.dao.entity.unique.key.StatCsAdvAllUniqueKey;
import com.codrim.cs.data.server.dao.param.DayStatCsAdvAllParam;
import com.codrim.cs.data.server.dao.param.SDateAndCompanyId;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author Liang.Zhuge on 14/07/2017.
 */
@Mapper
public interface StatCsAdvAllMapper extends StatCsMapper<StatCsAdvAllDO, StatCsAdvAllUniqueKey> {
    String tableName = "e0000_day_stat_cs_adv_all";

    // TODO: 调整,修复多货币数据下,dashboard无法显示的问题
    @Select("select " +
            "id, " +
            "sdate, " +
            "symbol, " +
            "advertiser_id advertiserId, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc " +
            "from " + tableName +
            " where sdate = #{param.sdate} and advertiser_id = #{param.companyId}")
    List<StatCsAdvAllDO> findBySDateAndAdvId(
            @Param("param") SDateAndCompanyId param,
            @Param("sZone") String zone);

    @Select("<script>" +
            "select" +
            " id, " +
            "sdate, " +
            "symbol, " +
            "advertiser_id advertiserId, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc " +
            "from " + tableName +
            " where sdate = #{param.sdate} and advertiser_id = #{param.companyId} " +

            "<choose>" +
            "<when test='param.advertiserManagerIdListSelected != null and param.advertiserManagerIdListSelected.size()!=0 and param.affiliateManagerIdListSelected != null and param.affiliateManagerIdListSelected.size()!=0'>" +
            " and (adv_affiliate_manager_id in " +
            "<foreach item='item' index='index' collection='param.affiliateManagerIdListSelected' open='(' separator=',' close=')'> #{item}</foreach>" +
            " or adv_advertiser_manager_id in " +
            "<foreach item='item' index='index' collection='param.advertiserManagerIdListSelected' open='(' separator=',' close=')'> #{item}" +
            "</foreach>" +
            ")</when>" +
            "<otherwise>" +
            "<when test='param.affiliateManagerIdListSelected != null and param.affiliateManagerIdListSelected.size()!=0'>" +
            " and adv_affiliate_manager_id in " +
            "<foreach item='item' index='index' collection='param.affiliateManagerIdListSelected' open='(' separator=',' close=') '>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='param.advertiserManagerIdListSelected != null and param.advertiserManagerIdListSelected.size()!=0'>" +
            " and adv_advertiser_manager_id in " +
            "<foreach item='item' index='index' collection='param.advertiserManagerIdListSelected' open='(' separator=',' close=') '>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "</otherwise> " +
            "</choose>" +

            "<if test='param.hasAllPermission==false'>"+
                "  and (1>2 "+
                    "<if test='param.advertiserManagerId != null'>"+
                    "   or adv_advertiser_manager_id = #{param.advertiserManagerId} "+
                    "</if>"+
                    "<if test='param.affiliateManagerId != null'>"+
                    "   or adv_affiliate_manager_id = #{param.affiliateManagerId} "+
                    "</if>"+
                    "<if test='param.advertiserManagerDepartmentIdSet != null and param.advertiserManagerDepartmentIdSet.size()!=0' >" +
                    "    or adv_advertiser_manager_department_id in " +
                    "    <foreach item='item' index='index' collection='param.advertiserManagerDepartmentIdSet' open='(' separator=',' close=')'>" +
                    "       #{item}" +
                    "    </foreach>" +
                    "</if>" +
                    "<if test='param.affiliateManagerDepartmentIdSet != null and param.affiliateManagerDepartmentIdSet.size()!=0' >" +
                    "    or adv_affiliate_manager_department_id in " +
                    "    <foreach item='item' index='index' collection='param.affiliateManagerDepartmentIdSet' open='(' separator=',' close=')'>" +
                    "       #{item}" +
                    "    </foreach>" +
                    "</if>" +
                 " )" +
            "</if> "+
            "</script>")
    List<StatCsAdvAllDO> findStatCsAdvAllForDashboard(
            @Param("param") DayStatCsAdvAllParam param,
            @Param("sZone") String zone);

    @Insert("insert into " + tableName + " (" +
            "sdate, " +
            "symbol, " +
            "advertiser_id, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc, " +
            "adv_advertiser_manager_id, " +
            "adv_affiliate_manager_id, " +
            "adv_advertiser_manager_department_id, " +
            "adv_affiliate_manager_department_id " +
            ") values (" +
            "#{param.sdate}, " +
            "#{param.symbol}, " +
            "#{param.advertiserId}, " +
            "#{param.click}, " +
            "#{param.uclick}, " +
            "#{param.gc}, " +
            "#{param.conversion}, " +
            "#{param.aprice}, " +
            "#{param.pprice}, " +
            "#{param.caps}, " +
            "#{param.sc}, " +
            "#{param.advAdvertiserManagerId}, " +
            "#{param.advAffiliateManagerId}, " +
            "#{param.advAdvertiserManagerDepartmentId}, " +
            "#{param.advAffiliateManagerDepartmentId} " +
            ")")
    void insert(@Param("param") StatCsAdvAllDO dayStatCsAdvAllInDB, @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Delete("delete from " + tableName + " where sdate = ${sdate}")
    void deleteSpecifiedDayReportData(@Param("sdate") int sdate, @Param("sZone") String zone);

    @Override
    @Select("<script> select " +
            "id, " +
            "sdate, " +
            "symbol, " +
            "advertiser_id advertiserId, " +
            "click, " +
            "uclick, " +
            "gc, " +
            "conversion, " +
            "aprice, " +
            "pprice, " +
            "caps, " +
            "sc, " +
            "adv_advertiser_manager_id, " +
            "adv_affiliate_manager_id, " +
            "adv_advertiser_manager_department_id, " +
            "adv_affiliate_manager_department_id " +
            "from " + tableName +
            " where  sdate = #{param.sdate} " +
            "<choose>" +
            "<when test='param.advertiserId !=null'> and advertiser_id = #{param.advertiserId} </when>" +
            "<otherwise> and advertiser_id is null </otherwise>" +
            "</choose>" +
            "<choose>" +
            "<when test='param.advAdvertiserManagerId !=null'> and adv_advertiser_manager_id = #{param.advAdvertiserManagerId} </when>" +
            "<otherwise> and adv_advertiser_manager_id is null </otherwise>" +
            "</choose>" +
            "<choose>" +
            "<when test='param.advAffiliateManagerId !=null'> and adv_affiliate_manager_id = #{param.advAffiliateManagerId} </when>" +
            "<otherwise> and adv_affiliate_manager_id is null </otherwise>" +
            "</choose>" +
            "<choose>" +
            "<when test='param.symbol !=null'> and symbol = #{param.symbol} </when>" +
            "<otherwise> and symbol is null </otherwise>" +
            "</choose>  limit 1 </script>")
    StatCsAdvAllDO findByKey(@Param("param") StatCsAdvAllUniqueKey key,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Override
    @Update("update " + tableName + " set " +
            "symbol = #{values.symbol}, " +
            "click = #{values.click}, " +
            "uclick = #{values.uclick}, " +
            "gc = #{values.gc}, " +
            "conversion = #{values.conversion}, " +
            "aprice = #{values.aprice}, " +
            "pprice = #{values.pprice}, " +
            "caps = #{values.caps}, " +
            "sc = #{values.sc} " +
            " where id = #{id}")
    void updateById(@Param("values") StatCsAdvAllDO dayStatCsAdvAllDO,
            @Param("id") Integer id,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Select("<script> select " +
            "sdate, " +
            "symbol, " +
            "sum(click) as click, " +
            "sum(uclick) as uclick, " +
            "sum(gc) as gc, " +
            "sum(conversion) as conversion, " +
            "sum(caps) as caps, " +
            "sum(sc) as sc," +
            "sum(aprice) aprice," +
            "sum(pprice) pprice " +
            "from " + tableName +
            " where sdate between #{begin} and #{end} " +
            "and advertiser_id = #{param.advertiserId}"+
            "<if test='param.hasAllPermission==false'>"+
                "  and (1>2 "+
                "<if test='param.advertiserManagerId != null'>"+
                "   or adv_advertiser_manager_id = #{param.advertiserManagerId} "+
                "</if>"+
                "<if test='param.affiliateManagerId != null'>"+
                "   or adv_affiliate_manager_id = #{param.affiliateManagerId} "+
                "</if>"+
                "<if test='param.advertiserManagerDepartmentIdSet != null and param.advertiserManagerDepartmentIdSet.size()!=0' >" +
                "    or adv_advertiser_manager_department_id in " +
                "    <foreach item='item' index='index' collection='param.advertiserManagerDepartmentIdSet' open='(' separator=',' close=')'>" +
                "       #{item}" +
                "    </foreach>" +
                "</if>" +
                "<if test='param.affiliateManagerDepartmentIdSet != null and param.affiliateManagerDepartmentIdSet.size()!=0' >" +
                "    or adv_affiliate_manager_department_id in " +
                "    <foreach item='item' index='index' collection='param.affiliateManagerDepartmentIdSet' open='(' separator=',' close=')'>" +
                "       #{item}" +
                "    </foreach>" +
                "</if>" +
                " )" +
            "</if> " +
            " group by sdate, symbol" +
            "</script>"
    )
    List<StatCsAdvAllDO> findByAdvIdAndPeriod(@Param("param") AdvertiserWholeDayDataForPeriodParamDTO param,
                                                @Param("begin") Integer begin,
                                                @Param("end") Integer end,
                                                @Param("sZone") String zone,
                                                @Param("timeDimesion") String timeDimesion);
}
